import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {TabsPage} from "../tabs/tabs";
import {AppGlobal, AppService} from "../../app/app.service";
import {Device} from "ionic-native";
import {Md5} from "ts-md5";
import { Buffer } from 'buffer';

/**
 * Generated class for the LogonPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-logon',
  templateUrl: 'logon.html',
})
export class LogonPage {
  //验证码倒计时
  verifyCode: any = {
    verifyCodeTips: "获取验证码",
    countdown: 20,//总共时间
    disable: true,
    phoneCode: "0000"//验证码
  };
  showflg: boolean = false;
  errormsg: any = "手机号码输入有误！";
  isChecked: boolean = false;
  user: any;//返回user

  constructor(public navCtrl: NavController, public navParams: NavParams,public appService: AppService) {

  }

  ionViewDidLoad() {

    console.log('ionViewDidLoad LogonPage');
  }

//倒计时
  settime() {
    if (this.verifyCode.countdown == 0) {
      this.verifyCode.verifyCodeTips = "获取验证码";
      this.verifyCode.disable = true;
      this.verifyCode.countdown = 20;
      return;
    } else {
      this.verifyCode.countdown--;
    }
    setTimeout(() => {
      this.verifyCode.verifyCodeTips = "验证码:" + this.verifyCode.phoneCode + "重发" + this.verifyCode.countdown + "秒";
      this.verifyCode.disable = false;
      this.settime();
    }, 1000);
  }

  /*获取验证码*/
  getCode() {
    this.verifyCode.phoneCode = '6666';
    this.settime();
  }

  /*电话号码校验*/
  blurtel(user_id: HTMLInputElement){
    if(user_id.value.length != 11 || !this.isDigit(user_id.value)) {
      this.showflg = true;
      this.errormsg = "手机号码输入错误";
    }else {
      this.showflg = false;
    }
  }
  /*判断一个字符串是否都为数字*/
  isDigit(strNum:string) {
  return strNum.match("[0-9]{1,}");
}

/*密码校验*/
  blurpassword(password: HTMLInputElement){

  }

  /*密码重复校验*/
  blurpassword2(password: HTMLInputElement,password2: HTMLInputElement){
    if(password.value != password2.value){
      this.showflg = true;
      this.errormsg = "两次密码输入不一致";
    }else {
      this.showflg = false;
    }
  }

  //注册
  logOn(username: HTMLInputElement, password: HTMLInputElement){
    let sysuuid = Device.uuid;
    let passwd = new Buffer(encodeURIComponent(password.value)).toString('base64');
    let md5 = Md5.hashStr(username.value + passwd + sysuuid).toString();
    var params = {user_id: username.value, passwd: passwd, sysuuid: sysuuid,md5:md5,
      sysversion:Device.version,manufacturer:Device.manufacturer,model:Device.model,platform:Device.platform,
      serial:Device.serial,isVirtual:Device.isVirtual};
    if(!this.isChecked){
      this.showflg = true;
      this.errormsg = "同意协议才可以注册!";
      return ;
    }
    let succ = 0;
    this.appService.httpPost(AppGlobal.API.logon, params, rs => {
      console.debug(rs);
      this.user = rs.data;
      succ = rs.state;
      console.debug(succ);
      if (succ){
          console.debug(succ);
          localStorage.setItem('loginsts',"1");//登录状态1-已登录0-退出
          localStorage.setItem('user',JSON.stringify(this.user));//保存用户信息对象
          this.navCtrl.setRoot(TabsPage);
        } else {
          localStorage.removeItem('passwd');
          localStorage.removeItem('username');
          localStorage.removeItem('loginsts');
          localStorage.removeItem('user');
        }
        //let modal = this.modalCtrl.create(TabsPage);
        //modal.present();
    });
  }

  /*是否同意协议*/
  onChange(){
    this.isChecked = !this.isChecked;
    this.showflg = false;
  }
  /*验证码是否输入正确*/
  isCode(code: HTMLInputElement) {
    if (code.value != this.verifyCode.phoneCode) {
      this.showflg = true;
      this.errormsg = "验证码输入错误";
    }else {
      this.showflg = false;
    }
  }
}
